<?php
/***
 * 
 * @author GuoLong
 * 2014/12/02
 * 
 */

class item_feedback extends Model {
	//
	private $table="xdf_item_feedback";
	private $fields=array("");
	//ѧ���ȡһ����ҵ����һ����ķ�������
	public function GetMyFeedbackByItemId($item_id,$homework_id,$user_id) {
		$sql = "SELECT `feedback` AS 'feedback' 
		FROM `$this->table` 
		WHERE `student_id`=$user_id AND `item_id`=$item_id AND `homework_id`=$homework_id";
		return $this->fetchRow( $sql );
	}
	//��ȡһ��ѧ����һ����ҵ�ж���ҵ������Ŀ�ķ���
	public function getHomeworkItemState($user_id,$homework_id){
		$sql = "SELECT `feedback` AS itemfeedback 
			FROM `$this->table` 
			WHERE `student_id`=$user_id AND `homework_id`=$homework_id";		
		return $this->fetchAll($sql);
	}
	//��ȡѧ����
	public function getFeedback($student_id,$homework_id,$item_id){
		$sql="SELECT * 
			FROM $this->table 
			WHERE `student_id`=$student_id  AND `item_id`=$item_id AND `homework_id`=$homework_id";

		return $this->_db->fetchRow($sql);
		
	}
	public function checkfeedback($user_id,$item_id){
		$sql="UPDATE $this->table set `checked`='1' WHERE `item_id`='$item_id'";
		return $this->update($sql);
	}
	//ѧ������Ŀ�ỹ�ǲ���
	public function feedback($feedback,$student_id,$homework_id,$item_id){
		//��ȷ���Ƿ��Ѿ�����
		$result=$this->getFeedback($student_id,$homework_id, $item_id);
		$id=$result["id"];
		if($id){//����Ѿ��з��������ԭ���ֶ�
			$sql="UPDATE $this->table SET `feedback`='$feedback'
				WHERE `id`=$id;";
			return $this->update($sql);
		}else{//���û�з�������뵽����
			$sql="INSERT INTO `$this->table`
					(`feedback`,`student_id`,`homework_id`,`item_id`,`create_time`)
					VALUES('$feedback','$student_id','$homework_id','$item_id',NOW());";
			return $this->insert($sql);
		}
	}
	//����һ������
	public function insertFeedback($feedback,$student_id,$homework_id,$item_id){
		$sql="INSERT INTO `$this->table`
		(`feedback`,`homework_id`,`student_id`,`item_id`,`create_time`)
		VALUES($feedback,$student_id,'$homework_id',$item_id,NOW());";
		return $this->_db->insert($sql);
	}
	//����һ����ķ�����Ϣ
	public function updateFeedback($feedback,$id){
		$sql="UPDATE $this->table SET `feedback`='$feedback'
		WHERE `id`=$id;";
		return $this->_db->update($sql);
	}
	//��ȡ�Ҳ������Ŀid
	//subject_id,start_datetime,end_datetime
	public function getCannotItemList($user_id,$timestamp,$pagesize,$action,$subject_id,$start_datetime,$end_datetime){
		
		$filter=array();
		if($action=='pre'){
			$filter["preOrMore_sql"]=" TRUE";
		}
		if($action=='more'){
			$filter["preOrMore_sql"]=" hf.`id` < $timestamp ";
		}
		//SET subject filter
		if($subject_id<>"0"){
			$filter["subject"]=" i.`sub_id`='$subject_id' ";
		}else{
			$filter["subject"]="TRUE";
		}
		//SET time filter
		if($start_datetime<>"0" && $end_datetime<>"0"){
			$filter["time"]=" hf.`create_time`>='$start_datetime' AND hf.`create_time`<='$end_datetime' ";
		}else{
			$filter["time"]="TRUE";
		}
		$sql="SELECT hf.`id`,hf.`item_id`,i.`content` as content,hf.`create_time`
				FROM `xdf_item_feedback` hf 
				LEFT JOIN `xdf_item` i ON i.`id`=hf.`item_id`
				WHERE hf.student_id=$user_id AND hf.checked=0 AND hf.create_time IN (
					SELECT max(create_time) 
					FROM `xdf_item_feedback` 
					WHERE `student_id`=$user_id AND item_id=hf.item_id ) AND" 
				.join(" AND ",$filter)." ORDER BY hf.`id` DESC LIMIT $pagesize";
		$rows=$this->fetchAll($sql);
		if($rows)
		foreach ($rows as $key=> $val){
			//replace html tags
			$item_content_tmp=strip_tags($val["content"]);
			$order   = array("\r\n", "\n", "\r");
			$replace = ' ';
			// Processes \r\n's first so they aren't converted twice. 
			$item_content = str_replace($order, $replace, $item_content_tmp);	
			$rows[$key]["content"]=mb_substr($item_content, 0, 40, 'utf-8');
		} 
		return $rows;
	}
	//获取不会题目总数（如果有重复的题目，多次统计）
	public function getCannotItemTotal($user_id){
		return $this->_db->getTotal($this->table,"`student_id`='$user_id' AND `feedback`='0'");
	}
	//获取做过的题目总数（如果有重复的题目，多次统计）
	public function getDoneItemTotal($user_id){
		return $this->_db->getTotal($this->table,"`student_id`='$user_id'");
	}
	//获取做过的作业总数
	public function getDoneHomeworkTotal($user_id){
		$sql="SELECT COUNT(`id`)as num FROM `xdf_homework` WHERE `id` IN ( SELECT `homework_id` FROM `xdf_item_feedback` WHERE `student_id`='$user_id');";
		$row=$this->fetchRow($sql);
		return $row["num"];
	}
	//获取学生作业总数
	public function getMyHomeworkTotal($student_id){
		$sql="SELECT COUNT(*) AS num FROM `xdf_homework_period` 
				WHERE `publish_type`=3 AND `period_id` IN (
					SELECT `id` AS period_id FROM `xdf_period` WHERE `class_id` IN(
						SELECT `class_id` FROM `xdf_class_student` WHERE `student_id`= '$student_id' 
					)
			);";
		$row=$this->fetchRow($sql);
		return $row["num"];
	}
}
